package com.mf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mf.bo.DateScopedBo;
import com.mf.entity.CodeHistory;
import com.mf.entity.PurchaseRecord;
import com.mf.mapper.PurchaseRecordMapper;
import com.mf.service.ICodeHistoryService;
import com.mf.service.IPurchaseRecordService;
import com.mf.utils.StockGatherUtils;
import com.mf.vo.KAndDateVo;
import com.mf.vo.PurchaseRecordVo;
import com.mf.vo.StockGatherVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 服务实现类
 *
 * @author mf
 * @since 2024-09-08
 */
@Service
public class PurchaseRecordServiceImpl extends ServiceImpl<PurchaseRecordMapper, PurchaseRecord> implements IPurchaseRecordService {
    @Resource
    private PurchaseRecordMapper purchaseRecordMapper;

    @Resource
    private ICodeHistoryService codeHistoryService;

    /**
     * 分页查询股票的K线图，并标识对应的买入卖出点
     * PurchaseRecordVo
     */
    @Override
    public List<PurchaseRecordVo> listPage(Integer pageNum, Integer pageSize) {
        final List<PurchaseRecord> page1 = purchaseRecordMapper.selectList(null);
        final List<PurchaseRecordVo> purchaseRecordVo = new ArrayList<>();

        List<PurchaseRecord> useList = page1.stream().skip((pageNum - 1) * pageSize).limit(pageSize).toList();

        final DateScopedBo dateScopedBo = new DateScopedBo();
        dateScopedBo.setStartDate(LocalDate.now().minusYears(8).toString());
        dateScopedBo.setEndDate(LocalDate.now().toString());

        // 按照股票代码进行分组
        final Map<String, List<PurchaseRecord>> collect = useList.stream().collect(Collectors.groupingBy(PurchaseRecord::getDm));
        for (Map.Entry<String, List<PurchaseRecord>> stringListEntry : collect.entrySet()) {

            final PurchaseRecordVo purchaseRecordVo1 = new PurchaseRecordVo();
            final List<CodeHistory> historyList = codeHistoryService.getHistoryList(dateScopedBo, stringListEntry.getKey(), "dn");
            final StockGatherVo stockGather = StockGatherUtils.getStockGather(historyList);

            List<KAndDateVo> dates = new ArrayList<>();

            for (PurchaseRecord purchaseRecord : stringListEntry.getValue()) {
                final KAndDateVo kAndDateVo = new KAndDateVo();
                kAndDateVo.setDate(purchaseRecord.getBuyDate());
                kAndDateVo.setPrice(purchaseRecord.getBuyPrice());
                kAndDateVo.setProfit(purchaseRecord.getBuyPrice());
                kAndDateVo.setColor("red");

                final KAndDateVo kAndDateVo1 = new KAndDateVo();
                kAndDateVo1.setDate(purchaseRecord.getSellDate());
                kAndDateVo1.setPrice(purchaseRecord.getSellPrice());
                kAndDateVo1.setColor("blue");
                kAndDateVo1.setProfit(purchaseRecord.getProfit());

                dates.add(kAndDateVo);
                dates.add(kAndDateVo1);
            }
            purchaseRecordVo1.setStockGatherVo(stockGather);
            purchaseRecordVo1.setKAndDateVo(dates);
            purchaseRecordVo.add(purchaseRecordVo1);
        }

        return purchaseRecordVo;
    }
}
